﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Security.Cryptography;

namespace BASE
{
    public class Utils
    {
        public static int getIntQuery(string id)
        {
            int number;
            if (HttpContext.Current.Request.QueryString[id] != null)
            {
                if ((!Int32.TryParse(HttpContext.Current.Request.QueryString[id].ToString(), out number)))
                {
                    return 0;
                }
                return int.Parse(HttpContext.Current.Request.QueryString[id].ToString());
            }
            return 0;
        }
        public static String getStringQuery(string id)
        {

            if (HttpContext.Current.Request.QueryString[id] != null)
            {

                return HttpContext.Current.Request.QueryString[id].ToString();
            }
            return "";
        }
        public static String getStringFrom(string id)
        {

            if (HttpContext.Current.Request.Form[id] != null)
            {

                return HttpContext.Current.Request.Form[id].ToString();
            }
            return "";
        }
        public static int getIntFrom(string id)
        {
            int number;
            if (HttpContext.Current.Request.Form[id] != null)
            {
                if ((!Int32.TryParse(HttpContext.Current.Request.Form[id].ToString(), out number)))
                {
                    return 0;
                }
                return int.Parse(HttpContext.Current.Request.Form[id].ToString());
            }
            return 0;
        }
        public static String getStringSession(String txt)
        {
            if (HttpContext.Current.Session[txt]!=null)
            {
                return HttpContext.Current.Session[txt].ToString();
            }
            return "";
        }
        public static int getIntSession(String txt)
        {
            int number;
            if (HttpContext.Current.Session[txt] != null)
            {
                if ((!Int32.TryParse(HttpContext.Current.Session[txt].ToString(), out number)))
                {
                    return Int32.Parse(HttpContext.Current.Session[txt].ToString());
                }
                return 0;
            }
            return 0;
        }

        public static string getMD5(string txt)
        {
            string source = txt;
            MD5 md5 = MD5.Create();
            string hash = GetMd5Hash(md5, source);
            return hash;
        }

        static string GetMd5Hash(MD5 md5Hash, string input)
        {

            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes 
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }

        // Verify a hash against a string. 
        static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
        {
            // Hash the input. 
            string hashOfInput = GetMd5Hash(md5Hash, input);

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
